package com.itstherules.jmsunit.expectation;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;

import com.itstherules.jmsunit.headers.JmsHeadersDefinition;

public final class JmsHeadersDefinitions {

	private HashMap<String, JmsHeadersDefinition> definitions = new HashMap<String, JmsHeadersDefinition>();

	public void add(JmsHeadersDefinitionImpl definition) {
		definitions.put(definition.getName(), definition);
	}

	public final JmsHeadersDefinition get(String definitionKey) {
		JmsHeadersDefinition definition = definitions.get(definitionKey);
		if (definition == null) {
			try {
				InputStream stream = JmsHeadersDefinitions.class.getClassLoader().getResourceAsStream("com/itstherules/jmsunit/message/definitions/"+definitionKey);
				Properties properties = new Properties();
				properties.load(stream);
				definition = new JmsHeadersDefinitionImpl(definitionKey, properties);
				definitions.put(definition.getName(), definition);
				stream.close();
			} catch (IOException e) {
				throw new AssertionError("There is no message definition for key: " + definitionKey);
			}
		}
		return definition;
	}

}
